from django.contrib.auth.hashers import make_password
from django.contrib.auth.models import AbstractUser
from django.db import models
from django.db.models import Q
from django.utils import timezone


# 定义用户模型
class User(AbstractUser):  # 模型继承自django自带的User模型 并在其基础上添加另外的属性

    # 手机号
    mobile = models.CharField(max_length=11, unique=True, blank=False, verbose_name='手机')  # blank=False 表示必须要填写该字段的值

    # 头像信息 头像的保存路径static/avatar/%Y%m%d
    avatar = models.ImageField(upload_to='media/avatar/%Y%m%d', blank=True,
                               default='media/avatar/20210602/demo.png', verbose_name='头像')  # blank=True 表示该字段的值可有可无

    # job
    job = models.CharField(max_length=20, default='打工的人儿~', verbose_name='职位')

    # 简介
    desc = models.CharField(max_length=300, blank=True, default='这个人很懒,ta什么都没写~', verbose_name='简介')

    # 是否是游客
    isVistor = models.BooleanField(default=1, db_column='是否是游客', verbose_name='是游客吗？')

    class Meta:
        db_table = '用户'  # 修改表名
        verbose_name_plural = verbose_name = db_table  # admin 后台显示

    # 只要自己定义了__str__(self)方法，那么就会打印从在这个方法中return的数据
    def __str__(self):
        return self.username

    @classmethod
    def get_by_id(cls, id):
        return cls.objects.filter(id=id).first()

    @classmethod
    def get_by_username(cls, username):
        return cls.objects.filter(username=username).first()

    @classmethod
    def get_by_username_and_mobile(cls, username, mobile):
        return cls.objects.filter(Q(username=username) | Q(mobile=mobile)).first()

    @classmethod
    def get_by_mobile(cls, mobile):
        return cls.objects.filter(mobile=mobile).first()

    @classmethod
    def update_user(cls, user, mobile, username, pwd, is_visitor):
        user.mobile = mobile
        user.username = username
        user.password = make_password(pwd)
        user.isVistor = is_visitor
        user.save()

    # 创建一个新的用户
    @classmethod
    def create_user(cls, mobile, password, username, isVistor=True):
        user = User(mobile=mobile, password=make_password(password), isVistor=isVistor,
                    username=username)  # 对密码进行加密 用户名同手机号
        user.save()
        return user

# 信息表
class messageTable(models.Model):
    # 发送人的id
    senderId = models.CharField(max_length=10, db_column='发送人的id', verbose_name='发送人的id')  # 发送人可以是匿名私信

    # 接收人的id
    receiverId = models.ForeignKey(User, on_delete=models.CASCADE, db_column='接收人的id', verbose_name='接收人')

    # 发送的内容
    content = models.CharField(max_length=300, db_column='发送的内容', verbose_name='发送的内容')

    # 申请日期
    messageTime = models.DateTimeField(default=timezone.now, db_column='发送日期', verbose_name='发送日期')

    # 是否已读
    isRead = models.BooleanField(default=False, db_column='是否已读', verbose_name='是否已读')

    # 是否显示
    isShow = models.BooleanField(default=1, db_column='是否显示', verbose_name='是否显示')

    class Meta:
        db_table = '信息'
        verbose_name = verbose_name_plural = db_table
        ordering = ['messageTime']


# 背景音乐
class BackgroundMusic(models.Model):
    # 歌名
    title = models.CharField(max_length=30, db_column='歌名', verbose_name='歌名')

    # 歌曲图片
    picture = models.ImageField(upload_to='imgs/%Y/%m/%d', db_column='歌曲图片', verbose_name='歌曲图片')

    # 歌曲地址
    file_path = models.FileField(upload_to='music/%Y/%m/%d', db_column='歌曲地址', verbose_name='歌曲地址')

    class Meta:
        db_table = '歌曲'
        verbose_name = verbose_name_plural = db_table

    @classmethod
    def get_all(cls):
        return cls.objects.values('title', 'picture', 'file_path')


# 留言(这个一定要登录之后才能留言)
class Message(models.Model):
    # 留言人
    user = models.ForeignKey(User, db_column='留言人', verbose_name='留言人', on_delete=models.CASCADE)

    # 留言内容
    content = models.CharField(max_length=100, db_column='留言内容', verbose_name='留言内容')

    # 留言时间
    date = models.DateTimeField(auto_now_add=True, db_column='留言时间', verbose_name='留言时间')

    # 权重
    weight = models.PositiveIntegerField(default=0, db_column='权重', verbose_name='权重')

    class Meta:
        db_table = '留言'
        verbose_name_plural = verbose_name = db_table
        ordering = ['-weight', '-date', ]

    @classmethod
    def get_all(cls):
        data_list = []
        for i in cls.objects.all():
            data_list.append(
                {
                    'name': i.user,
                    'avatar': i.user.avatar,
                    'content': i.content,
                    'date': ''.join(str(i.date).split('.')[:-1])

                }
            )
        return data_list

    @classmethod
    def save_message(cls):
        pass
